<?php
namespace zzcms\util;

class Curl
{
    /**
     * GET请求
     * @param $url  URL地址
     * @return bool|mixed
     */
    public static function get($url)
    {
        $curl = curl_init();
        if (stripos($url, "https://") !== false) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $content = curl_exec($curl);
        $status = curl_getinfo($curl);
        curl_close($curl);
        if (intval($status["http_code"]) == 200) {
            return $content;
        } else {
            return false;
        }
    }

    public static function gets($url)
    {
        $curl = curl_init();
        if (stripos($url, "https://") !== false) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $content = curl_exec($curl);
        $status = curl_getinfo($curl);
        curl_close($curl);
        return $content;
    }

    public static function getXml($url)
    {
        $curl = curl_init();
        if (stripos($url, "https://") !== false) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $content = curl_exec($curl);
        curl_close($curl);
        $xml = simplexml_load_string($content);
        if ($xml) {
            return $xml;
        } else {
            return false;
        }
    }

    /**
     * POST 请求
     * @param $url URL地址
     * @param $param  字符串或者数组
     * @param bool|false $post_file 是否上传文件
     * @return bool|mixed
     */
    public static function post($url, $param, $post_file = false)
    {
        $curl = curl_init();
        if (stripos($url, "https://") !== false) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1);
        }
        if (is_string($param) || $post_file) {
            $post_param = $param;
        } else {
            $param_array = [];
            foreach ($param as $key => $val) {
                $param_array[] = $key . "=" . urlencode($val);
            }
            $post_param = join("&", $param_array);
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_param);
        $content = curl_exec($curl);
        $status = curl_getinfo($curl);
        curl_close($curl);
        if (intval($status["http_code"]) == 200) {
            return $content;
        } else {
            return false;
        }
    }


    public static function getCurlFileMedia($file_path)
    {
        if (class_exists('\CURLFile')) {// 这里用特性检测判断php版本
            $data = new \CURLFile($file_path, "", "");//>=5.5
            $file_array = explode('/', $data->name);
            $data->postname = end($file_array);
        } else {
            $data = '@' . $file_path;//<=5.5
        }
        return $data;
    }


    public static function  curlFile($url, $data)
    {
        $curl = curl_init();
        if (class_exists('\CURLFile')) {
            curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
        } else {
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
            }
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, "TEST");
        $result = curl_exec($curl);
        $status = curl_getinfo($curl);
        curl_close($curl);
        if (intval($status["http_code"]) == 200) {
            return $result;
        } else {
            return false;
        }
    }

    /**
     * 生成安全JSON数据
     * @param array $array
     * @return string
     */
    public static function jsonEncode($array)
    {
        return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function('$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'), json_encode($array));
    }


    /**
     * 下载远程文件
     * @param $url  文件地址
     * @param $filename  保存的文件名（相对于网站根目录）
     * @return bool
     */
    public static function curlDownload($url, $filename)
    {
        if (empty($filename)) {
            return false;
        }
        if (false === strpos($url, 'http')) {
            return false;
        }
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 6);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $content = curl_exec($ch);
        curl_close($ch);
        if (!$content) {
            return false;
        }
        $filename_array = explode('/', $filename);
        $dir = str_replace(end($filename_array), '', $filename);
        if (!is_dir($dir)) {
            @mkdir($dir, 0777, true);
        }
        $resource = fopen($filename, 'a');
        fwrite($resource, $content);
        fclose($resource);
        return is_file($filename);
    }

}
